package com.titanrise.crm.environment.ioc;

import lombok.NoArgsConstructor;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @Description: 加载resource目录下的配置文件
 * @Author: pront
 * @Time:2024-11-02 12:33
 */
@NoArgsConstructor
public class ConfigLoader {

    private final Properties properties = new Properties();

    public ConfigLoader(String fileName) {
        try (InputStream input = getClass().getClassLoader().getResourceAsStream(fileName)) {
            if (input == null) {
                System.out.println("Sorry, unable to find " + fileName);
                return;
            }
            properties.load(input);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    // 获取属性值
    public String getProperty(String key) {
        return properties.getProperty(key);
    }

    // 可以添加重载方法来支持默认值
    public String getProperty(String key, String defaultValue) {
        return properties.getProperty(key, defaultValue);
    }

}
